home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-04-13 | 3.6 KB | 78 lines | [TEXT/GEOL] |
- Item 6829672 12-April-89 15:16
-
- From: ALGER Alger, Jeff
-
- To: MACAPP.TECH$ MACAPP Tech
-
- cc: FRIEDRICH1 Friedrich, Steve
-
- Sub: Re: Dangerous lock in 2.0ß7
-
- I agree wholeheartedly with Paul Smith on this one: lock counts are the "right"
- way to handle object locking. My reasons (not an exhaustive list) are as
- follows:
-
- • Semaphores are the tried and true mechanism for resource control in more
- traditional environments. I find no compelling reason to reinvent the concept
- in MacApp. Arguments of "overhead" fall flat with me: in a MacApp application
- a couple of bytes per object is a raindrop in a thunderstorm. Even if one
- objects to storing the reference count in each object (I certainly do not), the
- literature is full of techniques for efficiently maintaining the locks
- externally.
-
- • Use of a reference counter does not require keeping track of local variables
- to remember the state.
-
- • Having object A remember the prior state of object B somewhat goes against
- the grain of object-oriented programming.
-
- • On a purely subjective, personal note, I have used both techniques in the
- past and have found lock counts to be much easier to deal with.
-
- I might suggest two additional methods to add to Paul's list which facilitate
- error handlers:
-
- Function TObject.LockCount: Integer; { return the current lock count }
- Procedure TObject.ResetLockCount (newCount: Integer); { set lock count to
- newCount unconditionally }
-
- These are only to be used when there is a special requirement to restore state,
- such as with an error handler, and they are admittedly grubby. Does this
- really reduce to the technique used in 2.0ß9? Yes, but ONLY when you have a
- real need to remember state; in all other cases, you need not worry about the
- details. The best of both worlds!
-
- While the waters are boiling, I would like to humbly suggest an analogous
- extension to TObject which is simple to implement and tremendously useful: a
- "reference count" which is distinct from a lock count. When you have a class,
- each instance of which may be pointed at from a variety of places, there is
- always a big problem in freeing the instance exactly once and only after all
- references to it have been invalidated. By implementing two primitives, one
- can implement self-owned objects:
-
- Procedure TObject.Grab; { call whenever you store an object's handle }
- Procedure TObject.Release;{ call whenever you stop storing an object's handle}
-
- Grab increments the reference counter by one, while Release decrements it and,
- when it reaches zero, calls TObject.Free. This is benign in cases where Grab
- and Release are never called, so it may be used when appropriate. It also
- works well with inheritance insofar as Release may be overridden to do special
- cleanup first, then call Inherited Release or not as the situation dictates.
- Among the many benefits of this is the ability to breakpoint on these methods
- to watch the usage of an instance as a program runs! Another consequence is
- that Free can make sure its reference count is zero before continuing, thereby
- making sure that dangling references will not continue to point to an object
- after it has been freed.
-
- We have used this scheme with great success for particular classes and wish it
- were in TObject. In fact, we have taken the concept one step further by
- passing a "byWhom" argument to Grab and Release which is used in debug mode to
- maintain a list of objects which reference the one Grabbed. From the Inspector
- one can instantly see every object which references a given one at any time.
-
- Any comments?
-
- Jeff Alger
-
-
-